package com.android.antelopeapp.view.BeautyPic;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.android.antelopeapp.constant.ApiConfig;
import com.android.antelopeapp.external.paginglistview.PagingBaseAdapter;
import com.android.antelopeapp.network.GsonRequest;
import com.android.antelopeapp.network.NetworkManager;
import com.android.antelopeapp.view.MyPagingListView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lyhw.model.BeautyPic;
import com.lyhw.model.beanwraper.WraperFactory;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by phk on 15-4-13.
 * 可下拉的装载美图的列表控件
 */
public class BPicListView extends MyPagingListView<BeautyPic> {
    public BPicListView(Context context) {
        super(context);
    }

    public BPicListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BPicListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void initAdapter() {
        setAdapter(new BpicAdapter());
    }

    @Override
    protected void updateListView() {
        String url=ApiConfig.REQUEST_BEAUTY_PIC_LIST+"?"+ApiConfig.START_POS+"="+mStartPos;
        GsonRequest< WraperFactory.BPicListWraper> grequest = new GsonRequest<WraperFactory.BPicListWraper>(Request.Method.GET,
                url,
                WraperFactory.BPicListWraper.class,
                null,
                new Response.Listener<WraperFactory.BPicListWraper>() {
                    @Override
                    public void onResponse(WraperFactory.BPicListWraper response) {
                        addItemsToList(!response.getData().isLast(),response.getData().getContent());
                        addPage();
                        Log.d(TAG,"get bpic data success!");
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.getStackTraceString(error);
                    }
                }
        );
        NetworkManager.getInstance(bacty).getRequestQueue().add(grequest);
    }


    /**BpicAdapter*/
    public class BpicAdapter extends PagingBaseAdapter<BeautyPic> {

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            BPicView bpic = null;
            if(convertView == null){
                bpic = new BPicView(bacty,null);
            }else{
                bpic = (BPicView)convertView;
            }
            bpic.bindData(items.get(position));
            bpic.getTagableImage().setmInteractModel(TagableImage.NORMAL_MODE);
            return bpic;
        }
    }
}
